package test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author: yuanZhi...
 * @Date: created in  2021/12/19 16:24
 */
public class WordDragon {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String next = sc.next();
        System.out.println(next);
        List<String> list = new ArrayList<>();
        list.add("0");
        list.add("6");
        list.add("word");
        list.add("dd");
        list.add("da");
        list.add("dc");
        list.add("dword");
        list.add("ac");

        // 第一个单词
        int indexStart = Integer.parseInt(list.get(0));
        String firstWord = list.get(indexStart + 2);
        list.remove(firstWord);
        // 移除前两个元素
        list.remove(0);
        list.remove(0);

        while (list.size() > 0) {
            List<String> sameLengthWords = getSameLengthWords(firstWord, list);
            if (sameLengthWords.size() > 0) {
                // 找出字典序最小的字符串
                String minStr = getMinStr(sameLengthWords);
                firstWord += minStr;
                list.remove(minStr);
            } else {
                break;
            }
        }
        System.out.println(firstWord);
    }

    private static List<String> getSameLengthWords(String endWord, List<String> list) {
        String endItem = endWord.substring(endWord.length() - 1);
        int listSize = list.size();
        // 找出以结尾字母开头且长度相同的单词
        List<String> sameLengthWords = new ArrayList<>();
        int maxLength = 0;
        for (int i = 0; i < listSize; i++) {
            String str = list.get(i);
            if (str.length() > maxLength) {
                maxLength = str.length();
            }
        }
        for (int i = 0; i < listSize; i++) {
            String str = list.get(i);
            if (str.startsWith(endItem) && str.length() >= maxLength) {
                maxLength = str.length();
                sameLengthWords.add(str);
            }
        }
        return sameLengthWords;
    }

    private static String getMinStr(List<String> sameLengthWords) {
        String minStr = sameLengthWords.get(0);
        // 找出字典序最小的字符串
        for (int i = 1; i < sameLengthWords.size(); i++) {
            String str = sameLengthWords.get(i);
            if (str.compareTo(minStr) < 0) {
                minStr = str;
            }
        }
        return minStr;
    }
}
